package com.piotrs10.bsk.gui.views;

import java.util.ArrayList;
import java.util.Iterator;

import com.piotrs10.bsk.dao.KlientDao;
import com.piotrs10.bsk.dao.SamochodDao;
import com.piotrs10.bsk.dao.SprzedazDao;
import com.piotrs10.bsk.dao.TypKlientaDao;
import com.piotrs10.bsk.model.Klient;
import com.piotrs10.bsk.model.Samochod;
import com.piotrs10.bsk.model.Sprzedaz;
import com.piotrs10.bsk.model.TypKlienta;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class ModifyKlient extends ModifyLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = 964645586220071206L;
	private TextField imieField, nazwiskoField, adresField;
	private ComboBox typBox;

	public ModifyKlient(boolean isInsert, Integer id, Window modalWindow) {
		super(isInsert, modalWindow);

		this.imieField = new TextField("Imie");
		this.nazwiskoField = new TextField("Nazwisko");
		this.adresField = new TextField("Adres");
		this.typBox = new ComboBox("Typ");
		this.commitButton = new Button("Zapisz");

		this.loadCustomerTypes(isInsert, id);

		this.fieldsPanel.addComponent(this.imieField);
		this.fieldsPanel.addComponent(this.nazwiskoField);
		this.fieldsPanel.addComponent(this.adresField);
		this.fieldsPanel.addComponent(this.typBox);
		this.fieldsPanel.addComponent(this.commitButton);
	}

	private void loadCustomerTypes(boolean insert, Integer id) {
		// TODO: Pobrac z bazy danych co trzeba
		ArrayList<TypKlienta> typesList = new TypKlientaDao().getAll();
		for(TypKlienta current : typesList) {
			this.typBox.addItem(current.getId());
		}
		
		if(insert == false) {
			Klient k = new KlientDao().get(id);
			this.imieField.setValue(k.getImie());
			this.nazwiskoField.setValue(k.getNazwisko());
			this.adresField.setValue(k.getAdres());

			Iterator<?> iterator = this.typBox.getItemIds().iterator();
			Object item = null;
			int typId = 0;
			do {
				item = iterator.next();
				typId = Integer.valueOf(this.typBox.getItemCaption(item));
			} while(typId != k.getTyp());
			this.typBox.setValue(item);
		}
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO: zapisac dane w bazie

	}

	public static void fillTable(Table table) {
		table.addContainerProperty("ID", Integer.class, null);
		table.addContainerProperty("Imie", String.class, null);
		table.addContainerProperty("Nazwisko", String.class, null);
		table.addContainerProperty("Adres", String.class, null);
		table.addContainerProperty("Typ", Integer.class, null);
		
		ArrayList<Klient> list = new KlientDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Klient k = list.get(j);
			table.addItem(new Object[] { k.getId(), k.getImie(), k.getNazwisko(), 
					k.getAdres(), k.getTyp()}, j);
		}
	}
	
	public static void refresh(Table table) {
		table.removeAllItems();
		ArrayList<Klient> list = new KlientDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Klient k = list.get(j);
			table.addItem(new Object[] { k.getId(), k.getImie(), k.getNazwisko(), 
					k.getAdres(), k.getTyp()}, j);
		}
	}

	public String getWinWidth() {
		return "250px";
	}

	public String getWinHeight() {
		return "260px";
	}
}
